#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>


#define MAXLINE 800
#define SERV_PORT 8000

void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen)
{
 int n;
 int cnt;
 char sendline[MAXLINE], recvline[MAXLINE + 1];

 /* connect to server */
 if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1) {
    perror("connect error");
    exit(1);
 }

 while(fgets(sendline, MAXLINE, fp) != NULL) {
    /* read a line and send to server */
    cnt = strlen(sendline);
    sendline[cnt-1]='\0'; //remove CRLF
    write(sockfd, sendline, strlen(sendline));
    /* receive data from server */
    n = read(sockfd, recvline, MAXLINE);
    if(n == -1) {
        perror("read error");
        exit(1);
    }
    recvline[n] = '\n'; /* terminate string */
    recvline[n+1] = 0; /* terminate string */
    fputs(recvline, stdout);
 }
}

void main(int argc, char *argv[])
{
 int sockfd;
 struct sockaddr_in servaddr;
 const char *argv1;

 /* check args */
 if(argc != 2)
 {
 //printf("usage: udpclient <IPaddress>\n");
 //exit(1);
    argv1 = "113.140.20.98";
    printf("set IP=<%s>\n", argv1);
 } else {
    argv1 = argv[1];
 }

 /* init servaddr */
 bzero(&servaddr, sizeof(servaddr));
 servaddr.sin_family = AF_INET;
 servaddr.sin_port = htons(SERV_PORT);
 if(inet_pton(AF_INET, argv1, &servaddr.sin_addr) <= 0)
 {
 printf("[%s] is not a valid IPaddress\n", argv1);
 exit(1);
 }

 sockfd = socket(AF_INET, SOCK_DGRAM, 0);

 do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

 return;
}
